/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import TimeRecording.business.DateRange;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author dpatchett
 */
@Entity
@Table(name = "public_holiday")
@NamedQueries({
    @NamedQuery(
            name = "PublicHoliday.findAll",
            query = "SELECT ph " +
                    "FROM PublicHoliday ph " +
                    "ORDER BY ph.dateOfHoliday"),
    @NamedQuery(
            name = "PublicHoliday.findByHolidayId",
            query = "SELECT ph " +
                    "FROM PublicHoliday ph " +
                    "WHERE ph.holidayId = :holidayId"),
    @NamedQuery(
            name = "PublicHoliday.findByDateRange",
            query = "SELECT ph " +
                    "FROM PublicHoliday ph " +
                    "WHERE ph.dateOfHoliday >= :fromDate AND " +
                          "ph.dateOfHoliday <= :toDate " +
                    "ORDER BY ph.dateOfHoliday")})

public class PublicHoliday implements Serializable {

    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "holiday_id")
    private Integer holidayId;

    @Column(name = "date_of_holiday")
    @Temporal(TemporalType.DATE)
    private Date dateOfHoliday;

    @Column(name = "description")
    private String description;

    public PublicHoliday() {
    }

    public PublicHoliday(Integer holidayId) {
        this.holidayId = holidayId;
    }

    public Integer getHolidayId() {
        return holidayId;
    }

    public void setHolidayId(Integer holidayId) {
        Integer oldholidayId = this.holidayId;
        this.holidayId = holidayId;
        changeSupport.firePropertyChange("holidayId", oldholidayId, holidayId);
    }

    public Date getDateOfHoliday() {
        return dateOfHoliday;
    }

    public void setDateOfHoliday(Date dateOfHoliday) {
        Date oldDateOfHoliday = this.dateOfHoliday;
        this.dateOfHoliday = dateOfHoliday;
        changeSupport.firePropertyChange("dateOfHoliday", oldDateOfHoliday, dateOfHoliday);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        String oldDescription = this.description;
        this.description = description;
        changeSupport.firePropertyChange("description", oldDescription, description);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PublicHoliday other = (PublicHoliday) obj;
        if (this.holidayId != other.holidayId && (this.holidayId == null || !this.holidayId.equals(other.holidayId))) {
            return false;
        }
        if (this.dateOfHoliday != other.dateOfHoliday && (this.dateOfHoliday == null || !this.dateOfHoliday.equals(other.dateOfHoliday))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (this.holidayId != null ? this.holidayId.hashCode() : 0);
        hash = 41 * hash + (this.dateOfHoliday != null ? this.dateOfHoliday.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "TimeRecording.tables.PublicHoliday[holidayId=" + holidayId + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

    public static List<PublicHoliday> getAllHolidays(EntityManager entityManager) {
        Query query = entityManager.createNamedQuery("PublicHoliday.findAll");
        List<PublicHoliday> holidays = query.getResultList();
        return holidays;
    }

    public static List<PublicHoliday> getHolidaysForDateRange(
            EntityManager entityManager, DateRange dateRange) {

        Query query = entityManager.
                createNamedQuery("PublicHoliday.findByDateRange").
                setParameter("fromDate", dateRange.getFromDate()).
                setParameter("toDate", dateRange.getToDate());

        List<PublicHoliday> holidays = query.getResultList();
        return holidays;
    }
}